package cn.edu.cuit.aumalls.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.cuit.aumalls.entity.CartGoods;
import cn.edu.cuit.aumalls.entity.Goods;
import cn.edu.cuit.aumalls.service.CartService;
import en.edu.cuit.aumalls.mapper.CartgoodsMapper;
import en.edu.cuit.aumalls.mapper.FindGoodsMapper;
import en.edu.cuit.aumalls.mapper.GoodsMapper;
import en.edu.cuit.aumalls.mapper.OrderDetailMapper;
import en.edu.cuit.aumalls.mapper.OrdersMapper;

@Service("cartService")
public class CartServiceImpl implements CartService {

	@Autowired
	private GoodsMapper goodsMapper;
	
	@Autowired
	private CartgoodsMapper cartgoodsMapper;
	
	@Autowired
	private OrdersMapper ordersMapper;
	
	@Autowired
	private OrderDetailMapper orderDetailMapper;
	
	
	@Autowired
	private FindGoodsMapper findGoodsMapper;
	
	@Override
	public List<CartGoods> showCartGoods() {
		/*List<Goods> goodslist;*/
		List<CartGoods> cartgoodslist = cartgoodsMapper.findAll();
		
		/*goodslist = goodsMapper.findAll();*/
		
		/*for(int i=0;i<goodslist.size();i++) {
			CartGoods cartgoods = new CartGoods();
			float duty = 0;
			float realprice=0;
			float totalprice = 0;
			
			cartgoods.setGoods_id(goodslist.get(i).getGoods_id());
			cartgoods.setGoods_name(goodslist.get(i).getGoods_name());
			
			if(goodslist.get(i).getGoods_price() >50) {
				duty = (float) (goodslist.get(i).getGoods_price()*1*0.1);
			}
			else {
				duty = 0;
			}
			
			cartgoods.setGoods_duty(duty);
			cartgoods.setGoods_price(goodslist.get(i).getGoods_price());
			
			realprice = (float) (goodslist.get(i).getGoods_price()*0.8);
			cartgoods.setReal_price(realprice);
			cartgoods.setNum(1);
			
			totalprice = 1*realprice;
			cartgoods.setTotal_price(totalprice);
			
			System.out.println(cartgoods);
			cartgoodslist.add(cartgoods);
		}*/
		return cartgoodslist;
	}
	
	public List<Goods> showYouLike(int userId){
		String orderId = ordersMapper.findLastOrderIdByUser(userId);
		int goodsId = orderDetailMapper.findGoodsIdByOrderId(orderId);
		int goodsType = goodsMapper.findGoodsTypeByGoodsId(goodsId);
		List<Goods> goodslist = goodsMapper.findGoodsByType(goodsType);
		
		System.out.println(goodslist);
		return goodslist;
	}
	
	public List<Goods> showHotSale(){
		List<Integer> goodsIdList = orderDetailMapper.findDetailGoods();
		for(int i = 0; i< goodsIdList.size() -1;i++) {
			for(int j = goodsIdList.size() - 1; j > i; j--) {
				if(goodsIdList.get(j).equals(goodsIdList.get(i))) {
					goodsIdList.remove(j);
				}
			}		
		}
		/*System.out.println(goodsIdList);*/
		
		List<HotSale> hotSaleList = new ArrayList<>();
		for(int i =0; i< goodsIdList.size();i++) {
			HotSale hotSale = new HotSale();
			hotSale.setGoods_id(goodsIdList.get(i));
			hotSale.setGoods_hot(orderDetailMapper.countByDetailGoods(goodsIdList.get(i)));
			hotSaleList.add(hotSale);
		}
		Collections.sort(hotSaleList);
		/*System.out.println(hotSaleList);*/
		
		List<Goods> goodslist =  new ArrayList<>();
		for(int i=0; i< hotSaleList.size(); i++) {
			Goods goods = new Goods();
			goods = goodsMapper.findGoodsById(hotSaleList.get(i).getGoods_id());
			goodslist.add(goods);
		}
		
		/*System.out.println(goodslist);*/
		return goodslist;
	}
	
	class HotSale implements Comparable<HotSale>{
		private int goods_id;
		private int goods_hot;
		
		public int getGoods_id() {
			return goods_id;
		}
		public void setGoods_id(int goods_id) {
			this.goods_id = goods_id;
		}
		public int getGoods_hot() {
			return goods_hot;
		}
		public void setGoods_hot(int goods_hot) {
			this.goods_hot = goods_hot;
		}
		
		@Override
		public String toString() {
			return "HotSale [goods_id=" + goods_id + ", goods_hot=" + goods_hot + "]";
		}
		@Override
		public int compareTo(HotSale hotSale) {
			// TODO Auto-generated method stub
			return hotSale.getGoods_hot() - this.getGoods_hot();
		}		
	}

	
	public void deleteOneItem(int goodsId) {
		cartgoodsMapper.deleteOneByGoodsId(goodsId);
	}

	@Override
	public void addGoodsToCart(String goods_name) {
		float duty = 0;
		float realprice=0;
		float totalprice = 0;
		
		Goods goods = findGoodsMapper.findGoodsByName(goods_name).get(0);
		
		if(goods.getGoods_price()>50) {
			duty = (float) (goods.getGoods_price()*1*0.1);
		}
		else {
			duty = 0;
		}
		
		realprice = (float) (goods.getGoods_price()*0.8);
		
		totalprice = realprice * 1;
		
		cartgoodsMapper.addGoods(goods_name,duty,goods.getGoods_price(),realprice,1,totalprice);
		
	}
}
